<?php
namespace App\Models\Traits;

trait AppCate
{
	public function cate()  //分类
    {
        return $this->belongsTo(\App\Models\Cate::class, 'cate_id');
    }

    public function scopeCateId($query,$cate_id=0) //分类过滤，包括子节点
    {
		if($cate_id){
			$cate = get_app_cate(false,$cate_id);
			if(empty($cate)){
				return $query;
			}
			if(empty($cate['childs'])){
				return $query->where('cate_id', $cate['id']);
			}else{
				$ids = str2arr($cate['childs']);
				$ids[] = $cate['id'];
				return $query->whereIn('cate_id', $ids);
			}
		}else{
			return $query;
		}
    }

}
